#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef float GLfloat;
typedef int GLint;

#include "bunny.h"


int main(int argc,char* argv[])
{
	FILE* ofp;
	int i;
	int want_binary;
	
	if (argc != 3) {
		printf("Usage: %s -A|-B [OUTPUTFILE]\n",argv[0]);
		return 1;
	}

	if (!strcmp(argv[1],"-A")) {
		want_binary = 0;
	}
	else if (!strcmp(argv[1],"-B")) {
		want_binary = 1;
	}
	else {
		printf("%s: First argument must be -A or -B\n",argv[0]);
		return 1;
	}

	/* okay, lets do the work */
	if (!want_binary)
	{
		if ((ofp=fopen(argv[2],"w")) == NULL) {
			printf("%s: cannot open file for writing\n",argv[1]);
			return 1;
		}

		// header
		fprintf(ofp,"BMESH:A:VNT PT %d %d|",NUM_POINTS,NUM_TRIANGLES);
		// vertices
		for (i = 0; i < NUM_POINTS; i++)
			fprintf(ofp,"%f %f %f\n",bunny[3*i+0],bunny[3*i+1],bunny[3*i+2]);
		// normals
		for (i = 0; i < NUM_POINTS; i++)
			fprintf(ofp,"%f %f %f\n",normals[3*i+0],normals[3*i+1],normals[3*i+2]);
		// triangles
		for (i = 0; i < NUM_TRIANGLES; i++)
			fprintf(ofp,"%d %d %d\n",triangles[3*i+0],triangles[3*i+1],triangles[3*i+2]);
	}
	else
	{
		if ((ofp=fopen(argv[2],"wb")) == NULL) {
			printf("%s: cannot open file for writing\n",argv[1]);
			return 1;
		}

		// header
		fprintf(ofp,"BMESH:B:VNT PT %d %d|",NUM_POINTS,NUM_TRIANGLES);
		// vertices
		fwrite(bunny,sizeof(float),NUM_POINTS*3,ofp);
		// normals
		fwrite(normals,sizeof(float),NUM_POINTS*3,ofp);
		// triangles
		fwrite(triangles,sizeof(int),NUM_TRIANGLES*3,ofp);
	}
	
	fclose(ofp);
	return 0;
}

